home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / quartz / quartz10.lha / src / presto / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-02  |  2.2 KB  |  145 lines

  1. /*
  2.  *    misc.c
  3.  *
  4.  *     General mishmash that doesn't belong anywhere else
  5.  *    - fatalerror()        -- abort and die horribly
  6.  *    - overloaded new and delete
  7.  */ 
  8.  
  9. #define _MISC_C
  10.  
  11. #include <stream.h>
  12. #include <osfcn.h>
  13. #include "presto.h"
  14.  
  15. void
  16. error(char *s)
  17. {
  18.     cerr << s << "\n";
  19.     fatalerror();
  20. }
  21.  
  22. void
  23. fatalerror()
  24. {
  25.     cerr << "Aborting....\n";
  26.     cout.flush();
  27.     cerr.flush();
  28.     abort();
  29. }
  30.  
  31. //
  32. // We have to redefine the new and delete functions so they malloc
  33. // in shared memory.
  34. //
  35.  
  36. typedef void (*PFVV)();
  37.  
  38. extern PFVV _new_handler;
  39.  
  40. typedef char*    (*PFUC)(unsigned);        // for malloc
  41. typedef void    (*PFVC)(char*);            // for free
  42.  
  43. extern char* malloc(unsigned);
  44. extern char* realloc(char *, unsigned);
  45. extern void  free(char*);
  46.  
  47. //
  48. // The memory allocator should be called "malloc" in all
  49. // Presto versions.  That prevents the c-library malloc from being
  50. // linked in and called accidentally.  A call to the c-library
  51. // malloc will cause Topaz Presto to get blown out of the water.
  52. //
  53. #define MALLOC(x)        malloc(x)
  54. #define FREE(x)            free(x)
  55. PFUC    mallocf = malloc;
  56. PFVC    freef = free;
  57.  
  58. extern void* operator new(long size)
  59. {
  60.     char* p;
  61.  
  62.     while ( (p=MALLOC(unsigned(size)))==0 ) {
  63.         if(_new_handler)
  64.             (*_new_handler)();
  65.         else    
  66.             return 0;
  67.     }
  68.     return (void*)p;
  69. }
  70.  
  71.  
  72. extern void operator delete(void* p)
  73. {
  74.     if (p) FREE( (char*)p );
  75. }
  76.  
  77. //
  78. // Dangerous: If you do a set_{malloc/free}_agent from shared to non-shared
  79. // or the other way around, anything which has been malloced in one
  80. // form might get demalloced in another.  Of course, the classes
  81. // can remember this for themselves and make sure to set the
  82. // types correctly.
  83.  
  84. PFUC
  85. set_malloc_agent(PFUC nmalloc)
  86. {
  87.     PFUC omalloc = mallocf;
  88.  
  89.     mallocf = nmalloc;
  90.     return omalloc;
  91. }
  92.  
  93. PFVC
  94. set_free_agents(PFVC nfree)
  95. {
  96.     PFVC ofree = freef;
  97.     freef = nfree;
  98.     return ofree;
  99. }
  100.  
  101. void failed_malloc()
  102. {
  103.     cerr << "operator new failed: out of store\n";
  104.     fatalerror();
  105. }
  106.  
  107. void
  108. thisthread_holdingspinlock()
  109. {
  110.     thisthread->holdingspinlock();
  111. }
  112.  
  113. void
  114. thisthread_releasingspinlock()
  115. {
  116.     thisthread->releasingspinlock();
  117. }
  118.  
  119.  
  120. char*
  121. shmalloc(long s)
  122. {
  123.     return malloc(s);
  124. }
  125.  
  126. void
  127. shfree(char* x)
  128. {
  129.     free(x);
  130. }
  131. char *
  132. shrealloc(char* x, unsigned s)
  133. {
  134.     return realloc(x, s);
  135. }
  136.  
  137.  
  138. #ifdef sun
  139. int cpus_online() { return 1; }
  140. #endif sun
  141. #ifdef vax
  142. int cpus_online() { return 1; }
  143. #endif vax
  144.  
  145.